package com.netty.netty_webSocket;

import io.netty.channel.ChannelInitializer;
import io.netty.channel.ChannelPipeline;
import io.netty.channel.socket.SocketChannel;
import io.netty.handler.codec.http.HttpObjectAggregator;
import io.netty.handler.codec.http.HttpServerCodec;
import io.netty.handler.codec.http.websocketx.WebSocketServerProtocolHandler;
import io.netty.handler.stream.ChunkedWriteHandler;

public class webSocketServerInitializer extends ChannelInitializer<SocketChannel> {

    @Override
    protected void initChannel(SocketChannel ch) throws Exception {
        ChannelPipeline pipeline = ch.pipeline();
        /**
         * 添加处理器
         *
         *  websocket是http的所以需要有http的解码器
         */

        pipeline.addLast(new HttpServerCodec());
        /**
         * 以块的方式写
         */
        pipeline.addLast(new ChunkedWriteHandler());
        /**
         * Http协议，聚合 请求和响应。
         *
         * HttpObject
         *
         * 默认请求和响应是分段的，HttpObjectAggregator是聚合成完整的请求和响应。
         */
        pipeline.addLast(new HttpObjectAggregator(8192));

        /**
         *  websocket处理器。
         *  websocket定义的协议。
         *  websocket的url:
         *      ws://localhost:9999/content_path
         *  参数是content_path.
         */
        pipeline.addLast(new WebSocketServerProtocolHandler("/wwss"));

        /**
         *  添加自定义Handler
         */
        pipeline.addLast(new webSocketServerHandler());

    }
}
